<<<<<<< HEAD


=======
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
<<<<<<< HEAD
=======
import com.itheima.dao.SetmealDao;
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
<<<<<<< HEAD
=======
import com.itheima.pojo.Setmeal;
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
<<<<<<< HEAD
import java.util.*;

=======
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10

/**
 * 体检预约服务
 */
<<<<<<< HEAD


=======
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
<<<<<<< HEAD

    //体检预约
    public Result order(Map map) throws Exception {


/**
 1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
 2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
 3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
 4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
 5、预约成功，更新当日的已预约人数
 */

=======
    @Autowired
    private SetmealDao setmealDao;

    //体检预约
    public Result order(Map map) throws Exception {
        /**
         1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
         2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
         3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
         4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
         5、预约成功，更新当日的已预约人数
         */
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10

        //获取日期
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);//根据日期查询预约设置信息
        if (orderSetting == null) {
            //所选日期没有提前进行预约设置，不能完成预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            //所选日期已经约满，无法预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //判断是否在重复预约
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if (member != null) {
            Integer memberId = member.getId();//会员id
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));//套餐id
            Order order = new Order(memberId, date, setmealId);
            List<Order> orderList = orderDao.findByCondition(order);
            if (orderList != null && orderList.size() > 0) {
                //用户在重复预约，不能完成预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }

        if (member == null) {
            //当前用户不是会员，需要自动完成注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        //保存预约信息
        Order order = new Order(member.getId(),
                date,
                (String) map.get("orderType"),
                Order.ORDERSTATUS_NO,
                Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);

        //更新已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    //根据id查询预约详细信息（包括会员姓名、套餐名称、预约基本信息）
    public Map findById(Integer id) {
        Map map = orderDao.findById4Detail(id);
        if (map != null) {
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            try {
                map.put("orderDate", DateUtils.parseDate2String(orderDate));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

<<<<<<< HEAD
    public Result orderFromBackend(Map map, Integer[] setmealIds) throws Exception {
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
=======
    @Override
    public Result orderFromBackend(Map map, Integer[] setmealIds) throws Exception {
        String orderDate = (String) map.get("orderDate");
        System.out.println(orderDate);
        orderDate = orderDate.replace("Z", " UTC");
        System.out.println(orderDate);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        Date date = format.parse(orderDate);
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
        System.out.println(date);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);//根据日期查询预约设置信息
        if (orderSetting == null) {
            //所选日期没有提前进行预约设置，不能完成预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            //所选日期已经约满，无法预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //判断是否在重复预约
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        List<Order> orderResult = new ArrayList<>();
        if (member != null) {
            Integer memberId = member.getId();//会员id
            for (Integer setmealId : setmealIds) {
                Order order = new Order(memberId, date, setmealId);
                List<Order> orderList = orderDao.findByCondition(order);
                if (orderList != null && orderList.size() > 0) {
                    //用户在重复预约，不能完成预约
                    return new Result(false, MessageConstant.HAS_ORDERED);
                } else {
                    orderResult.add(saveOrder(member.getId(), date, Order.ORDERTYPE_TELEPHONE, Order.ORDERSTATUS_NO, setmealId, orderSetting));
                }
            }
        }

        if (member == null) {
            //当前用户不是会员，需要自动完成注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        //保存预约信息
        for (Integer setmealId : setmealIds) {
            orderResult.add(saveOrder(member.getId(), date, Order.ORDERTYPE_TELEPHONE, Order.ORDERSTATUS_NO, setmealId, orderSetting));
        }
        return new Result(true, MessageConstant.ORDER_SUCCESS, orderResult);
    }

<<<<<<< HEAD
    @Override
    public List<Map<Map<String,Integer>,Map<String,Integer>>> findOrderdateAndCount() throws Exception {
        List<Map<Map<String,Integer>,Map<String,Integer>>> list = new ArrayList<>();
        Map<Map<String,Integer>,Map<String,Integer>> dateAndCountMap = new HashMap<>();
        Map<String,Integer> visitsMap = new HashMap<>();
        Map<String,Integer> orderMap = new HashMap<>();
        List<Date> orderdate = orderDao.findOrderdate();
        for (Date date : orderdate) {
            if (date!=null) {
                String s = DateUtils.parseDate2String(date, "yyyy-MM-dd");
                Integer visitsCount = orderDao.findVisitsCountByDate(s);
                Integer orderCount = orderDao.findOrderCountByDate(s);
                visitsMap.put(s,visitsCount);
                orderMap.put(s,orderCount);
                dateAndCountMap.put(visitsMap,orderMap);
                list.add(dateAndCountMap);
                System.out.println(s + " " + visitsCount + " " + orderCount);
            }
        }
        return list;
    }


=======
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
    private Order saveOrder(Integer id, Date date, String ordertypeTelephone, String orderstatusNo, Integer setmealId, OrderSetting orderSetting) {
        Order order = new Order(id,
                date,
                ordertypeTelephone,
                orderstatusNo,
                setmealId);
        orderDao.add(order);

        //更新已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return null;
    }

<<<<<<< HEAD
    public List<Order> findHistoryById(Map map, Integer id) {
=======
    @Override
    public List<Map<String,Object>> findHistoryById(Map map, Integer id) {
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
        String beginTime = (String) map.get("beginTime");
        String endTime = (String) map.get("endTime");
        if (beginTime != null && endTime != null && id != null) {
            Map<String,Object> condition = new HashMap<>();
            condition.put("beginTime",beginTime);
            condition.put("endTime",endTime);
            condition.put("id",id);
<<<<<<< HEAD
            List<Order> orders = orderDao.findHistoryById(condition);
            return orders;
=======
            Member member = memberDao.findById(id);
            String memberName = member.getName();
            List<Order> orders = orderDao.findHistoryById(condition);
            List<Map<String,Object>>  result = new ArrayList<>();
            for (Order order : orders) {
                Map<String,Object> map1=new HashMap<>();
                Integer setmealId = order.getSetmealId();
                Setmeal setmeal = setmealDao.findById4Detail(setmealId);
                String setmealName = setmeal.getName();
                map1.put("memberName",memberName);
                map1.put("setmealName",setmealName);
                map1.put("id",order.getId());
                result.add(map1);
            }

            return result;
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
        }
        return null;
    }
}
<<<<<<< HEAD

=======
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
